package com.example.springboot.common;

import lombok.Data;

/**
 * 同一返回给前端的数据包装类
 */
@Data
public class Result {

    private String code;
    private Object data;
    private String msg;

    //返回成功的信息:不带参数
    public static Result success() {
        Result result = new Result();
        result.setCode(Constants.SUCCESS_CODE);
        result.setMsg(Constants.SUCCESS_MSG);
        return result;
    }

    //返回成功的信息:带参数
    public static Result success(Object data) {
        Result result = new Result();
        result.setCode(Constants.SUCCESS_CODE);
        result.setData(data);
        result.setMsg(Constants.SUCCESS_MSG);
        return result;
    }

    //返回失败的信息
    public static Result error(String msg) {
        Result result = new Result();
        result.setCode(Constants.ERROR_CODE);
        result.setMsg(msg);
        return result;
    }

    public static Result error(String code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }
}